package image.resizer.ui;

import java.text.MessageFormat;
import java.util.ResourceBundle;

public class MessageFormater {
	private ResourceBundle messages;
	
	public MessageFormater(ResourceBundle resourceBundle){
		if(resourceBundle == null){
			throw new IllegalArgumentException();
		}
		messages = resourceBundle;
	}

	public String getString(String key){
		if(messages == null){
			return key; // no mapping - return the key
		}

		String message = null;
		try{
			message = messages.getString(key);
		} 
		catch(Throwable t){}

		if(message == null){
			return key; // not found apropriate value for a key, return key itself
		}
		return message;
	}
	
	public String getString(String key, Object param){
		return getString(key, new Object[]{param});
	}

	public String getString(String key, Object param1, Object param2){
		return getString(key, new Object[]{param1, param2});
	}
	
	public String getString(String key, Object[] params){
		return MessageFormat.format(getString(key), params);
	}
}
